<template>
  <a-modal :title="title" style="top: 100px;" :visible="true" width="1300px" @cancel="handleCancel">
    <div class="tips">
    </div>
    <a-table class="product-table" :rowKey="row => row.id" :dataSource="list" bordered :pagination="false" size="small">

      <a-table-column title="序号" width="20px">
        <template slot-scope="text, record, index">
          <div>{{ index+1 }}</div>
        </template>
      </a-table-column>

      
      <a-table-column title="任务时间" width="100px">
        <template slot-scope="row">
          <div>
            {{row.start_time}}至 {{row.end_time}}
          </div>
        </template>
      </a-table-column>
      <a-table-column title="抽奖人数" width="50px">
        <template slot-scope="row">
          <div>{{row.all_num}}</div>
        </template>
      </a-table-column>
      <a-table-column title="记录" width="50px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleEditTask(row)">任务列表</a>
        </template>
      </a-table-column>
      <a-table-column title="备注" width="50px">
        <template slot-scope="row">
          <div>{{row.remark}}</div>
        </template>
      </a-table-column>
      <a-table-column title="创建时间" width="100px">
          <template slot-scope="row">
            <span>{{row.created_at}}</span>
          </template>
        </a-table-column>
        <a-table-column title="更新时间" width="100px">
          <template slot-scope="row">
            <span>{{row.updated_at}}</span>
          </template>
        </a-table-column>
      <a-table-column title="操作" width="120px">
        <template slot-scope="row">
          <a href="javascript:;" @click="handleEdit(row)">编辑</a>
          <a-divider type="vertical" />
           <a href="javascript:;" @click="handleDelete(row)">删除</a>
        </template>
      </a-table-column>
    </a-table>
    <template slot="footer">
      <div class="btn-c">
        <a-button type="primary" class="add" @click="handleCreate">
          新增一个计划任务
        </a-button>
        <div style="flex-grow: 1;"></div>

        <a-button key="back" @click="handleCancel">关闭</a-button>
        <a-modal title="任务列表" style="top: 100px;" :visible="isEditTaskLog" width="1200px"
           @cancel="isEditTaskLog = false" :footer="null">
           <Tasklog v-if="isEditTaskLog" :participant="participant" @cancel="isEditTaskLog = false" @success="initList"></Tasklog>
        </a-modal>
      </div>
    </template>
  </a-modal>
</template>
<script>
  import UploadImages from '@/components/UploadImages/UploadImages'
  import iSwitch from '@/components/ISwitch/ISwitch'
  import ISkuType from '@/components/ISkuType/ISkuType'
  import Tasklog from "./components/Tasklog.vue"
  export default {
    components: {
      UploadImages,
      iSwitch,
      ISkuType,
      Tasklog
    },
    props: {
      info: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    data() {
      return {
        isEditTaskLog: false,
        participant: '',
        list: [],
        listWeight: '',
        frame: [
          {
            title: '抽奖人数',
            key: 'all_num',
            type: 'number',
            tips: '只会选择普通的机器人!',
            newline: true,
            required: true,
            default: 100,
          },
          {
            title: '备注',
            type: 'textarea',
            key: 'remark',
            span: 22,
          },
        ]
      }
    },
    computed: {
      title() {
        return '编辑计划任务'
      }
    },
    watch: {
      uuid() {
        this.initList()
      },
    },
    created() {
      this.initList()
    },
    methods: {
      initList() {
        this.$http.get(`/robot_lottery`, {
          params: {
            activity_id: this.info.id
          }
        }).then(res => {
          this.list = res.data.list
        })
      },
      handleCreate() {
        this.$iForm.show({
          title: '新增计划任务',
          frame: this.frame,
          success: (data) => {
            this.$http.post(`/robot_lottery`, {
              ...data,
              activity_id: this.info.id,
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleEditTask(row) {
        this.participant = row.task
        this.isEditTaskLog = true
      },
      handleEdit(row) {
        this.$iForm.show({
          title: '编辑计划任务(每次编辑后都会重新生成任务列表!请注意!不要随便保存!)',
          frame: this.frame,
          form: row,
          success: (data) => {
            this.$http.put(`/robot_lottery/${row.uuid}`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.initList()
              this.$iForm.hide()
            })
          },
        })
      },
      handleCancel() {
        this.$emit('refresh')
        this.$emit('close')
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {

            this.$http.delete(`/robot_lottery/${row.uuid}`).then(res => {
              this.$message.success('删除成功')
              this.initList()
            })

          }
        })


      }
    }
  }
</script>
<style lang="less" scoped>
  .thumb {
    width: 60px;
    // height: 60px;
    border-radius: 6px;
  }

  .btn-c {
    display: flex;
    // margin-top: 30px;
    padding: 0rpx 30rpx;

    .add {}

    .save {
      margin-right: 20px;
    }

    .cancel {}
  }

  .tips {
    color: gray;
    font-size: 80%;
    margin-bottom: 10px;
  }

  a-input {
    display: inline;
  }
</style>
